package com.forum.common.event;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 点赞事件
 */
@Getter
@Setter
@NoArgsConstructor
public class LikeEvent extends MessageEvent {
    
    /**
     * 目标标题（帖子标题或评论内容摘要）
     */
    private String targetTitle;
    
    /**
     * 父资源ID（如评论所属的帖子ID）
     */
    private Long parentResourceId;
    
    /**
     * 构造一个帖子点赞事件
     * 
     * @param senderId 点赞用户ID
     * @param receiverId 帖子作者ID
     * @param postId 帖子ID
     * @param postTitle 帖子标题
     * @return 帖子点赞事件
     */
    public static LikeEvent postLike(Long senderId, Long receiverId, Long postId, String postTitle) {
        LikeEvent event = new LikeEvent();
        event.setType(EventType.POST_LIKE);
        event.setSenderId(senderId);
        event.setReceiverId(receiverId);
        event.setResourceId(postId);
        event.setTargetTitle(postTitle);
        event.setContent("有人点赞了您的帖子《" + postTitle + "》");
        event.setResourceType("POST");
        event.setLink("/post/detail/" + postId);
        return event;
    }
    
    /**
     * 构造一个评论点赞事件
     * 
     * @param senderId 点赞用户ID
     * @param receiverId 评论作者ID
     * @param commentId 评论ID
     * @param commentContent 评论内容摘要
     * @param postId 所属帖子ID
     * @return 评论点赞事件
     */
    public static LikeEvent commentLike(Long senderId, Long receiverId, Long commentId, String commentContent, Long postId) {
        LikeEvent event = new LikeEvent();
        event.setType(EventType.POST_LIKE); // 复用帖子点赞类型
        event.setSenderId(senderId);
        event.setReceiverId(receiverId);
        event.setResourceId(commentId);
        event.setParentResourceId(postId);
        event.setTargetTitle(commentContent);
        event.setContent("有人点赞了您的评论\"" + truncateContent(commentContent, 20) + "\"");
        event.setResourceType("COMMENT");
        event.setLink("/post/detail/" + postId + "#comment-" + commentId);
        return event;
    }
    
    /**
     * 截断内容到指定长度，并添加省略号
     */
    private static String truncateContent(String content, int maxLength) {
        if (content == null || content.length() <= maxLength) {
            return content;
        }
        return content.substring(0, maxLength) + "...";
    }
} 